<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css" type="text/css" media="all" />
<script src="<?php echo URL::base() ?>media/js/jquery-1.10.1.min.js"></script>

<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css" media="all" />
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>	 

<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>

<style type="text/css">
.display_div{display:none}//隐藏
.uppercase{text-transform:uppercase;}//大写字母

</style>

<script type="text/javascript">

$(document).ready(function(){
	$.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
	$(".datepicker").datepicker();
})
</script>
<style>
.asd{width:80px; text-align:right; display:inline-block; margin-right:4px;}
.table th{ font-weight:normal;text-align:center;}
.table th, .table td{ border:1px solid #ffffff;}
table tbody tr:nth-child(2n){ background-color:#f5f5f5;}
table tfoot tr{ background-color:#eeeeee;}
</style>
<!-- 查询 -->
<div>
<form action="" method="post" id="myfrom">
<div style="margin-bottom:10px;">
	<span class="asd">采购日期从:</span>
	<input style="width:164px;margin-left:-4px;" type="text" class="datepicker" readonly="readonly" name="purchaseorder_date_from" id="purchaseorder_date_from">
到 <input  style="width:164px;" type="text" class="datepicker" value="<?php echo date("Y-m-d");?>" name="purchaseorder_date_to"
readonly="readonly" id="purchaseorder_date_to">
</div><div style="margin-bottom:10px;">
<span class="asd">付款日期从:</span><input  style="width:164px;" type="text" class="datepicker" readonly="readonly"
name="pay_date_from" id="pay_date_from">
到 <input type="text" style="width:164px;" class="datepicker" value="<?php echo date("Y-m-d");?>" name="pay_date_to"
readonly="readonly" id="pay_date_to">
<!-- 排序
<select name="order_by" id="order_by">
<option value="1">收货日期</option>
<option value="2">SKU</option>
<option value="3">供应商</option>
<option value="4">采购数量</option>
<option value="5">到货数量</option>
</select> -->
</div><div style="margin-bottom:10px;">
<span class="asd">供应商:</span><input style="width:164px;" type="text" name="supplier_name" id="supplier_name">
</div><div style="margin-bottom:10px;">
<span class="asd">采购员:</span><input style="width:164px;" type="text" name="purchaser_name" id="purchaser_name">
<input type="hidden" name="per_page" value="50" id="per_page">
<input type="hidden" name="current_page" value="0" id="current_page">
<button style="margin-left:10px;" type="button" id="search_btn" class="btn btn-info">查询</button>
<button type="reset"  class="btn btn-inverse">重置</button>
<button type="button" id="search_load" class="display_div btn btn-success">下载</button>
</div>
</form>
</div>

<!-- 查询结果表头 -->
<div >
<table class="table table-bordered" id="table_content" style="border-collapse:collapse;">
<thead>
<tr style=" background-color:#4EA392;color:#fff;">
<th style="width:30px;">序号
</th>
<th style="width:90px;">采购员
</th>
<th style="width:90px;">采购日期
</th>
<th style="width:65px;">采购单号
</th>
<th>供应商
</th>
<th>供应商编号
</th>
<th  style="min-width:30px;">付款方式
</th>
<th>采购物品总金额
</th>
<th>运费
</th>
<th style="width:65px;">该段时间付款总额
</th>
<th style="width:60px;">采购单付款总额
</th>
<th>采购单状态
</th>
<th  style="width:60px;">第一次到货时间
</th>
</tr>
</thead>
<tbody id="div_content">
</tbody>
<tfoot>
<td>合计</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td> </td>
</tfoot>
</table>
</div>

<div>
总共<span id="sum_ids">0</span>条记录
<button id="add_more" class="display_div btn">还剩<span id="left_ids">0</span>条记录</button>
</div>
<div><span></span></div>



<script type="text/javascript"> 

uri = "";

//获取GET得uri
function get_uri_select()
{
	var purchaseorder_date_from = $.trim($("#purchaseorder_date_from").val());
	var purchaseorder_date_to = $.trim($("#purchaseorder_date_to").val());
	var pay_date_from = $.trim($("#pay_date_from").val());
	var pay_date_to = $.trim($("#pay_date_to").val());	
	var supplier_name = $.trim($("#supplier_name").val());
	var purchaser_name = $.trim($("#purchaser_name").val());

	var select = "";
	select += "?purchaseorder_date_from="+purchaseorder_date_from;	
	select += "&purchaseorder_date_to="+purchaseorder_date_to;
	select += "&pay_date_from="+pay_date_from;
	select += "&pay_date_to="+pay_date_to;
	select += "&supplier_name="+supplier_name;
	select += "&purchaser_name="+purchaser_name;

	return select;
}

function get_uri_page()
{
	var page = "";
	page += "&limit="+$("#per_page").val();
	page += "&offset=" +$("#current_page").val();

	return page;
}

//获取查询结果
function get_ajax(uri)
{

	var re = {};
	
	$.ajax({		
 		type:'get',
		url:'<?php echo url::base(true);?>finance/select'+uri,
 		dataType:'html',
 		async:false,
 		success:function(data)
 		{
 	 		re = data;	
 		},
 		error:function()
 		{
 			
 		}
 	});

 	return re;
}


//获取合计信息
function get_ajax_sum(uri)
{

	var re = {};
	
	$.ajax({		
		type:'get',
		url:'<?php echo url::base(true);?>finance/select_sum'+uri,
		dataType:'json',
		async:false,
		success:function(data)
		{
	 		re = data;	
		},
		error:function()
		{
			
		}
	});

	return re;
}

//显示查看更多
function show_more_btn()
{
	var sum_ids = parseInt($("#sum_ids").text());
	var per = parseInt($("#per_page").val());
	var current_page = parseInt($("#current_page").val());
	var left = sum_ids - current_page;

	$("#left_ids").text(left);
	
	if(left > 0)
	{
		$("#add_more").removeClass('display_div');
	}
	else
	{
		$("#add_more").addClass('display_div');
	}
}

//下载按钮
function show_search_load()
{

	var sum_ids = parseInt($("#sum_ids").text());
	if( (sum_ids > 10000) || (sum_ids <= 0) )
	{
		$("#search_load").addClass('display_div');
	}
	else 
	{
		$("#search_load").removeClass('display_div');
	}
}

//追加表格
function append_table(data)
{
	$("#div_content").append(data);
}

//清空表格
function empty_table()
{
	uri = "";
	
	$("#current_page").val(0);
	$("#sum_ids").text(0);
	$("#div_content").empty();
}

//设置当前页的下一页
function set_pagination_next()
{
	$("#current_page").val(parseInt($("#current_page").val())+parseInt($("#per_page").val()));
}



$("#add_more").click(function(){

	var page = get_uri_page();
	
	var re = get_ajax(uri+page);

	if(re)
	{
		append_table(re);
		set_pagination_next();
		show_more_btn();
		
	}
	
});

$("#search_btn").click(function(){

	empty_table();
	
	uri =  get_uri_select();
	var page = get_uri_page();
	
	
	var re = get_ajax(uri+page);
	
	var sum = get_ajax_sum(uri);
	
	if(sum)
	{
		$("#sum_ids").text(sum.count_ids);	
	}

	if(re && sum)
	{
		append_table(re);
		set_pagination_next();
		show_more_btn();
		show_search_load();
	}
});

//下载
$("#search_load").click(function()
{
	if(uri)
	{
		window.location.href = "<?php echo url::base(true)?>finance/load"+uri;
	}
	
});
</script>
